package edu.neusoft.android.yuejian14.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import edu.neusoft.android.yuejian14.Listener.RetrofitListener;
import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.javabean.Train;
import edu.neusoft.android.yuejian14.javabean.User;
import edu.neusoft.android.yuejian14.model.TrainModel;
import edu.neusoft.android.yuejian14.model.UserModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ApplyActivity extends BaseActivity implements View.OnClickListener,RetrofitListener {
    private TextView train_name,train_type,train_price,username,tel,train_notice,train_money;
    private ImageButton back;
    private Button submit;
    private int trainid;
    private int uid;

    @Override
    protected void initViews(){
        setLayout(R.layout.activity_apply);
        train_name= (TextView) findViewById(R.id.tr_name);
        train_type= (TextView) findViewById(R.id.tr_type);
        train_price= (TextView) findViewById(R.id.tr_price);
        username= (TextView) findViewById(R.id.s_name);
        tel= (TextView) findViewById(R.id.u_tel);
        train_notice= (TextView) findViewById(R.id.tr_notice);
        train_money= (TextView) findViewById(R.id.t_money);
        back=(ImageButton) findViewById(R.id.btn_back);
        submit=(Button) findViewById(R.id.submit);
        uid= BaseActivity.getUid();
    }

    @Override
    View initViews(View view) {
        return view;
    }

    @Override
    public void initEvents(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        uid = sp.getInt("uid", 0);

        UserModel userModel = new UserModel();
        Call<User> call = userModel.getUserInfo(uid);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();
                username.setText(user.getUsername());
                tel.setText(user.getUserphone());
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrainModel trainModel = new TrainModel();
                Call<Train> call = trainModel.addTrain(uid,trainid);
                call.enqueue(new Callback<Train>() {
                    @Override
                    public void onResponse(Call<Train> call, Response<Train> response) {
                        Train train = response.body();
                        if (train.getSuccess().equals("0")){
                            Toast.makeText(ApplyActivity.this,"报名失败",Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(ApplyActivity.this,"报名成功",Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Train> call, Throwable t) {

                    }
                });
            }
        });

    }

    @Override
    void initData() {
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        trainid=bundle.getInt("trainid");

        TrainModel trainModel = new TrainModel();
        trainModel.getTrainById(String.valueOf(trainid),this);
    }

    @Override
    public void onSuccess(Object object, int flag) {
                    Train train = (Train) object;
                     train_name.setText(train.getTrainname());
                    train_type.setText(train.getTrainlevel());
                    train_notice.setText(train.getTrainnotice());
                    train_price.setText(train.getTrainmoney()+"元");
                    train_money.setText("￥ "+ train.getTrainmoney()+"元");
    }

    @Override
    public void onFail() {

    }

    @Override
    public void onClick(View view) {

    }

}
